﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SIMS_GUI_Item
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.btn_PassWordOpenEyes.Hide();
            Win32.AnimateWindow(this.Handle, 1000, 0x60004);
        }

        private void pnl_MainLogin_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pnl_Login_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //实例化Graphics，对象g
            Color FColor = Color.FromArgb(173, 198, 238);
            //颜色1
            Color TColor = Color.FromArgb(79, 92, 144);
            //颜色2 
            Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal);
            //实例化刷子，第一个参数指示上色区域，第二个和第三个参数分别渐变颜色的开始和结束，第四个参数表示颜色的方向。
            g.FillRectangle(b, this.ClientRectangle);
            //进行上色
        }

        private void pnl_Cover_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //实例化Graphics，对象g
            Color FColor = Color.FromArgb(41, 52, 80);
            //颜色1
            Color TColor = Color.FromArgb(60, 79, 109);
            //颜色2 
            Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.BackwardDiagonal);
            //实例化刷子，第一个参数指示上色区域，第二个和第三个参数分别渐变颜色的开始和结束，第四个参数表示颜色的方向。
            g.FillRectangle(b, this.ClientRectangle);
            //进行上色
        }

        /// <summary>
        /// 学生登录选择按钮
        /// </summary>
        private bool IsAdmin = false;
        private void btn_Student_Click(object sender, EventArgs e)
        {
            IsAdmin = false;
            this.btn_Student.BackColor = Color.FromArgb(151, 175, 209);
            this.btn_Admin.BackColor = Color.FromArgb(140, 152, 204);
        }

        /// <summary>
        /// 管理员登录选择按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Admin_Click(object sender, EventArgs e)
        {
            IsAdmin = true;
            this.btn_Admin.BackColor = Color.FromArgb(151, 175, 209);
            this.btn_Student.BackColor = Color.FromArgb(140, 152, 204);
        }

        /// <summary>
        /// 登录按钮
        /// </summary>
        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        private void btn_Login_Click(object sender, EventArgs e)
        {
            string UserName = this.txt_UserName.Text;
            string PassWord = this.txt_PassWord.Text;
            var sql = string.Format("select count(1) from UserInformation where UserName='{0}' and PassWord='{1}'",UserName,PassWord);
            CheckNull(UserName, PassWord);
            int number = Convert.ToInt32(db.ExecScalar(sql));
            if (IsAdmin== false)
            {
                if (number > 0)
                {
                    /*MessageBox.Show("登录成功");*/
                    StudentManagementAccount ac = new StudentManagementAccount(txt_UserName.Text, txt_PassWord.Text);
                    ac.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
            }
            if (IsAdmin == true)
            {
                if (number > 0)
                {
                    /*MessageBox.Show("登录成功");*/
                    AdminManagementAccount ac = new AdminManagementAccount(txt_UserName.Text, txt_PassWord.Text);
                    ac.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
            }
        }
        

        //判断UserName和PassWord是否为空
        public bool CheckNull(string UserName,string PassWord)
        {
            if (string.IsNullOrWhiteSpace(UserName))
            {
                DialogResult dr = MessageBox.Show("请输入账号", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                this.txt_UserName.Focus();
                return true;
            }
            if (string.IsNullOrWhiteSpace(PassWord))
            {
                DialogResult dr = MessageBox.Show("请输入密码", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                this.txt_PassWord.Focus();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 隐藏密码
        /// </summary>
        public bool IsOpen = false;
        private void btn_PassWordOpenEyes_Click(object sender, EventArgs e)
        {
            if ((btn_PassWordOpenEyes.Focused) != IsOpen)
            {
                this.btn_PassWordOpenEyes.Hide();
                this.txt_PassWord.PasswordChar = '•';
                this.btn_PassWordCloseEyes.Show();
                this.txt_PassWord.Focus();
            }
            if ((btn_PassWordCloseEyes.Focused) != IsOpen)
            {
                this.btn_PassWordCloseEyes.Hide();
                this.txt_PassWord.PasswordChar = '\0';
                this.txt_PassWord.Focus();
            }
        }

        /// <summary>
        /// 隐藏密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PassWordCloseEyes_Click(object sender, EventArgs e)
        {
            if ((btn_PassWordOpenEyes.Focused) != IsOpen)
            {
                this.btn_PassWordOpenEyes.Hide();
                this.txt_PassWord.PasswordChar = '•';
                this.txt_PassWord.Focus();
            }
            if ((btn_PassWordCloseEyes.Focused) != IsOpen)
            {
                this.btn_PassWordCloseEyes.Hide();
                this.txt_PassWord.PasswordChar = '\0';
                this.btn_PassWordOpenEyes.Show();
                this.txt_PassWord.Focus();
            }
        }

        /*
        public class Win32
        {
            public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口
            public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口
            public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口
            public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口
            public const Int32 AW_CENTER = 0x00000010; //若使用了AW_HIDE标志，则使窗口向内重叠；若未使用AW_HIDE标志，则使窗口向外扩展。
            public const Int32 AW_HIDE = 0x00010000; //隐藏窗口，缺省则显示窗口。
            public const Int32 AW_ACTIVATE = 0x00020000; //激活窗口。在使用了AW_HIDE标志后不要使用这个标志。
            public const Int32 AW_SLIDE = 0x00040000; //使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略。
            public const Int32 AW_BLEND = 0x00080000; //使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool AnimateWindow(
          IntPtr hwnd, // handle to window 
              int dwTime, // duration of animation 
              int dwFlags // animation type 
              );
        }
        */

        public class Win32
        {
            /// <summary>
            /// 执行动画
            /// </summary>
            /// <param name="whnd">控件句柄</param>
            /// <param name="dwtime">动画时间</param>
            /// <param name="dwflag">动画组合名称</param>
            /// <returns>bool值，动画是否成功</returns>
            [DllImport("user32.dll")]
            public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);

            /// <summary>
            /// 从左到右显示
            /// </summary>
            public const Int32 AW_HOR_POSITIVE = 0x00000001;
            /// <summary>
            /// 从右到左显示
            /// </summary>
            public const Int32 AW_HOR_NEGATIVE = 0x00000002;
            /// <summary>
            /// 从上到下显示
            /// </summary>
            public const Int32 AW_VER_POSITIVE = 0x00000004;
            /// <summary>
            /// 从下到上显示
            /// </summary>
            public const Int32 AW_VER_NEGATIVE = 0x00000008;
            /// <summary>
            /// 若使用了AW_HIDE标志，则使窗口向内重叠，即收缩窗口；否则使窗口向外扩展，即展开窗口
            /// </summary>
            public const Int32 AW_CENTER = 0x00000010;
            /// <summary>
            /// 隐藏窗口，缺省则显示窗口
            /// </summary>
            public const Int32 AW_HIDE = 0x00010000;
            /// <summary>
            /// 激活窗口。在使用了AW_HIDE标志后不能使用这个标志
            /// </summary>
            public const Int32 AW_ACTIVATE = 0x00020000;
            /// <summary>
            /// 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略
            /// </summary>
            public const Int32 AW_SLIDE = 0x00040000;
            /// <summary>
            /// 透明度从高到低
            /// </summary>
            public const Int32 AW_BLEND = 0x00080000;
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND);
        }

        /// <summary>
        /// 最小化点击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Hidden_Click(object sender, EventArgs e)
        {
            this.btn_Hidden.BackColor = Color.FromArgb(200, 151, 175, 209);
            this.Close();
        }

        /// <summary>
        /// 关闭点击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.btn_Cancel.BackColor = Color.FromArgb(200, 151, 175, 209);
            this.Close();
        }

        /// <summary>
        /// 鼠标悬浮变色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Hidden_MouseMove(object sender, MouseEventArgs e)
        {
            this.btn_Hidden.BackColor = Color.FromArgb(200, 151, 175, 209);
        }

        /// <summary>
        /// 鼠标离开变色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Hidden_MouseLeave(object sender, EventArgs e)
        {
            this.btn_Hidden.BackColor = Color.FromArgb(200, 59, 73, 106);
        }

        /// <summary>
        /// 鼠标悬浮变色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_MouseMove(object sender, MouseEventArgs e)
        {
            this.btn_Cancel.BackColor = Color.FromArgb(200, 151, 175, 209);
        }

        /// <summary>
        /// 鼠标悬浮变色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_MouseLeave(object sender, EventArgs e)
        {
            this.btn_Cancel.BackColor = Color.FromArgb(200, 59, 73, 106);
        }
    }
}
